


package yf.mall.controller;
import com.yf.request.SearchParam;

import java.util.List;

import com.yf.response.RRException;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yf.annotation.SysLog;
import yf.mall.entity.MallGoodsEntity;
import yf.mall.entity.UserEntity;
import yf.mall.service.MallGoodsService;
import yf.mall.service.ShopService;
import yf.mall.service.UserService;
import yf.mall.vo.LiveVo;
import yf.mall.service.LiveService;
import com.yf.response.R;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
                                        
/**
 * 直播Controller
 *
 * @author hxf
 * @email 505860922@qq.com
 * @date Thu Sep 10 11:00:34 CST 2020
 */
@RestController
@RequestMapping("/live")
public class LiveController {
    @Autowired
    private LiveService liveService;
    @Autowired
    private UserService userService;
    @Autowired
    private MallGoodsService mallGoodsService;
    /**
     * 列表直播
     */
    @SysLog("查看直播")
    @RequestMapping("/list")
    @RequiresPermissions("live:list")
    public R list(@RequestBody SearchParam<LiveVo> params) {
        //条件查询
        String userCode = params.getEntity().getLive().getUserCode();
        if (userCode != null && !"".equals(userCode)){
            UserEntity userEntity = userService.findByCode(userCode);
            if (userEntity == null){
                throw new RRException("该编号不存在");
            }
            params.getEntity().getLive().setUserId(userEntity.getId());
        }
        PageHelper.startPage(params.getPage(), params.getLimit()).setOrderBy(params.getOrder());
        List<LiveVo> list = liveService.findVo(params.getEntity());
        if (list != null && list.size() > 0){
            for (LiveVo liveVo : list) {
                if (liveVo.getLive().getUserId() != null ){
                    UserEntity userEntity = userService.get(liveVo.getLive().getUserId());
                    liveVo.getLive().setUserCode(userEntity.getCode());
                }
            }
        }
        //获取所有上架的产品
        List<MallGoodsEntity> goods = mallGoodsService.findGoods();
        return R.ok(new PageInfo(list)).putExtra("goods",goods);
    }

    /**
 * 下载订单
 */
    @SysLog("下载订单")
    @RequestMapping("/down")
    @RequiresPermissions("live:list")
    public void down(@RequestBody SearchParam<LiveVo> params, HttpServletResponse response) {
        List<LiveVo> list = liveService.findVo(params.getEntity());
            liveService.downLoad(response, list);
    }


    /**
     * 查看直播
     */
    @SysLog("查看直播")
    @RequestMapping("/info/{id}")
    @RequiresPermissions("live:info")
    public R info(@PathVariable("id") Long id) {
            LiveVo vo = liveService.getVo(id);
            if (vo.getLive().getUserId() != null){
                UserEntity userEntity = userService.get(vo.getLive().getUserId());
                vo.getLive().setUserCode(userEntity.getCode());
            }
        //查找对应的产品
        List<Long> goods = liveService.findByLiveId(id);
        return R.ok(vo).putExtra("goods",goods);
    }

    /**
     * 保存直播
     */
    @SysLog("保存直播")
    @RequestMapping("/save")
    @RequiresPermissions("live:save")
    public R save(@RequestBody LiveVo vo) {
        liveService.saveVo(vo);
        return R.ok(vo);
    }

    /**
     * 修改直播
     */
    @SysLog("修改直播")
    @RequestMapping("/update")
    @RequiresPermissions("live:update")
    public R update(@RequestBody LiveVo vo) {
            liveService.updateVo(vo);

        return R.ok(vo);
    }

    /**
     * 删除直播
     */
    @SysLog("删除直播")
    @RequestMapping("/delete")
    @RequiresPermissions("live:delete")
    public R delete(Long[]ids) {
            liveService.deleteByLiveId(ids);
        return R.ok();
    }

    /**
     * 所有直播
     */
    @SysLog("所有直播")
    @RequestMapping("/all")
    public R all(String order) {
        PageHelper.orderBy(order!=null ? order : "id desc");
        List<LiveVo> list = liveService.findVo(null);
        return R.ok(list);
    }

    /**
     * 所有enums
     */
    @SysLog("所有enums")
    @RequestMapping("/dmz/options")
    public R options() {
        HashMap<String,Object> options = new HashMap();
                                                                                                                                                                                                                        return R.ok(options);
    }





    /***********自定义开始***********/
}
